package com.kust.orderService.service.impl;

import com.kust.orderService.client.EduClient;
import com.kust.orderService.client.UcenterClient;
import com.kust.orderService.entity.Order;
import com.kust.orderService.mapper.OrderMapper;
import com.kust.orderService.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.orderService.utils.OrderNoUtil;
import com.kust.serviceBase.exceptionHandler.GuliException;
import com.kust.serviceBase.orderVo.CourseWebVoOrder;
import com.kust.serviceBase.orderVo.memberOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String memberIdByJwtToken) {
        if (memberIdByJwtToken.isEmpty()){
            throw new GuliException(200001,"请先登录账户");
        }
        //通过远程调用获取用户信息
        memberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberIdByJwtToken);

        //通过远程调用根据课程id获取课信息
        CourseWebVoOrder courseInfoOder = eduClient.getCourseInfoOder(courseId);

        //创建order对象向 order对象里面放数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOder.getTitle());
        order.setCourseCover(courseInfoOder.getCover());
        order.setTeacherName(courseInfoOder.getTeacherName());
        order.setTotalFee(courseInfoOder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
